﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Api.Controllers.BASE;
using PeiYue.Common.HttpContextUser;
using PeiYue.IServices.Crm;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.Model.Models.Tms;

namespace PeiYue.Api.Controllers.Tms
{
    /// <summary>
    /// 线路门店管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class RouterShopController : BaseController<RouterShopDao>
    {
        readonly IRouterShopServices _routerShopServices;
        readonly IShopServices _shopServices;
        readonly IRouterServices _routerServices;
        readonly IUser _user;
        readonly IMapper _mapper;
        public RouterShopController
            (IRouterShopServices routerShopServices,
            IShopServices shopServices,
            IRouterServices routerServices,
            IUser user, IMapper mapper):base(routerShopServices,user)
        {
            _routerShopServices = routerShopServices;
            _shopServices = shopServices;
            _routerServices = routerServices;
            _user = user;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取门店线路分页列表
        /// </summary>
        /// <param name="requestDto">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<MessageModel<PageModel<SearchRouterShopResponseDto>>> GetList([FromQuery]SearchRouterShopRequestDto requestDto)
        {
            var data = await _routerShopServices.GetPage(requestDto);
            return new MessageModel<PageModel<SearchRouterShopResponseDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }
        /// <summary>
        /// 新增门店线路
        /// </summary>
        /// <param name="routerShopDto">新增门店线路参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Add([FromBody] UpdateRouterShopDto routerShopDto)
        {
            var data = new MessageModel<string>();
            int status = (int)CommonEnum.Status.Enable;
            var routerDao = (await _routerServices.Query(t => t.id == routerShopDto.router_id && t.status == status)).FirstOrDefault();
            if (routerDao != null)
            {
                var exceptRouterIdList = (await _routerServices.Query(t => t.type == routerDao.type && t.status == status)).Select(t => t.id).ToList();
                var dao = (await _routerShopServices.Query(t => exceptRouterIdList.Contains(t.router_id) && t.shop_id == routerShopDto.shop_id && t.status == status)).FirstOrDefault();
                if (dao == null)
                {
                    dao = _mapper.Map<RouterShopDao>(routerShopDto);
                    dao.create_user = _user.ID;
                    dao.update_user = _user.ID;
                    dao.id = await _routerShopServices.GenId();
                    var shopDao = await _shopServices.QueryById(routerShopDto.shop_id);
                    if (shopDao != null)
                    {
                        dao.shop_code = shopDao.codec;
                    }
                    else
                    {
                        dao.shop_code = "";
                    }
                    await _routerShopServices.Add(dao);
                }
                else
                {
                    routerDao = (await _routerServices.Query(t => t.id == dao.router_id && t.status == status)).FirstOrDefault();
                    if (routerDao.id != routerShopDto.router_id)
                    {
                        data.success = false;
                        data.status = 500;
                        data.msg = $"该门店已经在[{routerDao.names}]线路中,请先从[{routerDao.names}]中删除再添加";
                        return data;
                    }
                    else
                    {
                        dao.router_id = routerShopDto.router_id;
                        dao.customer_id = routerShopDto.customer_id;
                        dao.shop_id = routerShopDto.shop_id;
                        dao.expand_time = routerShopDto.expand_time;
                        dao.update_user = _user.ID;
                        dao.update_time = DateTime.Now;
                        await _routerShopServices.Update(dao);
                    }
                }
                data.success = true;
                data.msg = "添加成功";
                return data;
            }
            data.success = false;
            data.status = 500;
            data.msg = "线路无效";
            return data;

        }
    }
}